# Import the datetime module to handle date and time objects, specifically for default timestamps.
import datetime

# Import the base Model class from Flask-AppBuilder, which this model will inherit from.
from flask_appbuilder import Model
# Import various column types from SQLAlchemy to define the database schema.
from sqlalchemy import Column, DateTime, Integer, String, Text

# Import the Flask application instance to access its configuration.
from myapp.app import app
# Import a custom audit mixin for tracking creation and modification details.
from myapp.models.helpers import AuditMixinNullable

# Get the metadata object from the base Model class. Metadata stores information about the database schema.
metadata = Model.metadata
# Get the application's configuration object.
conf = app.config


# Define the License class, which maps to the 'license' table in the database.
# This model represents a software license, containing information about its terms, validity, and scope.
class License(Model, AuditMixinNullable):
    # Specifies the name of the database table for this model.
    __tablename__ = 'license'
    # Defines the primary key column for the table.
    id = Column(Integer, primary_key=True)

    # Defines a column for the name of the license, possibly the license holder or a description.
    name = Column(String(256), nullable=True, comment='名称')
    # Defines a column to store a unique identifier for the device or machine the license is tied to.
    device_fingerprint = Column(String(1024), nullable=True, default='', comment='设备指纹')
    # Defines a column for the type of license (e.g., 'trial', 'commercial', 'academic').
    type = Column(String(256), nullable=False, comment='授权类型')
    # Defines a column for the licensing mode (e.g., 'per-user', 'per-device', 'subscription').
    mode = Column(String(256), nullable=False, comment='授权模式')
    # Defines a column for the current status of the license (e.g., 'active', 'expired', 'revoked').
    status = Column(String(256), nullable=False, comment='授权状态')
    # Defines a column for the date when the license becomes effective.
    effective_date = Column(
        DateTime, nullable=False, default=datetime.datetime.now, comment='生效日期'
    )
    # Defines a column for the date when the license expires.
    expired_date = Column(
        DateTime, nullable=False, default=datetime.datetime.now, comment='过期日期'
    )
    # Defines a column to store a list of models or features that are covered by this license, likely as a JSON string or comma-separated list.
    licensed_models = Column(Text(65536), nullable=True, default='', comment='已授权的模型')
    # Defines a column to store the license key or secret itself.
    secret = Column(Text(65536), nullable=True, default='', comment='密钥')

    # Defines the official string representation of a License instance.
    def __repr__(self):
        """
        Returns a string representation of the model instance, primarily for debugging.
        This should return a string, so it should be `str(self.id)`.
        """
        return self.id
